package com.develop.data.loan.jpa.attribute.converter;


import com.develop.data.loan.model.LoanContract;
import com.develop.data.loan.repository.LoanContractRepository;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class LoanContractAttributeConverter implements AttributeConverter<LoanContract, String> {

    @Autowired
    LoanContractRepository loanContractRepository;

    @Override
    public String convertToDatabaseColumn(LoanContract loanContract) {
        return loanContract == null ? null: loanContract.getId();
    }

    @Override
    public LoanContract convertToEntityAttribute(String id) {
        if (id == null) return null;
        return loanContractRepository.findById(id).orElse(null);
    }
}

